<?php


namespace app\shopConsole\controller;


use app\BaseController;
use app\lib\exception\ParamException;
use app\model\ShopUser;
use app\Request;
use app\shopConsole\service\user\UserQueryService;
use app\shopConsole\service\user\UserService;
use app\shopConsole\validate\UserValidate;
use think\App;
use think\exception\ValidateException;

function user2dict($list)
{
    $dateTime = app('DateTime');

    if (!empty($list['level']))
    {
        $list['level'] = [
            'id' => $list['level']['id'],
            'name' => $list['level']['name'],
        ];
    }

    if (!empty($list['role']))
    {
        empty($list['roleInfo']) ? $roleInfo = [] : $roleInfo = [
            'name' => $list['roleInfo']['realname'],
            'gender' => $list['roleInfo']['gender'],
            'birthdate' => $list['roleInfo']['birthdate'],
            'phone' => $list['roleInfo']['phone'],
            'id_card_number' => $list['roleInfo']['id_card_number'],
            'e_name' => $list['roleInfo']['e_name'],
            'e_province' => $list['roleInfo']['e_province'],
            'e_city' => $list['roleInfo']['e_city'],
            'e_district' => $list['roleInfo']['e_district'],
            'e_address' => $list['roleInfo']['e_address'],
            'e_owner_name' => $list['roleInfo']['e_owner_name'],
            'e_owner_phone' => $list['roleInfo']['e_owner_phone'],
            'e_tel' => $list['roleInfo']['e_tel'],
        ];

        $list['role'] =[
            'id' => $list['role']['id'],
            'name' => $list['role']['name'],
            'info' => $roleInfo,
        ];
    }

    if (!empty($list['referrer']))
    {
        $list['referrer'] = [
            'id' => $list['referrer']['id'],
            'name' => $list['referrer']['nickname'],
        ];
    }

    return [
        'id' => $list['id'],
        'nickname' => $list['nickname'],
        'headimg' => $list['headimg'],
        'tags' => explode(',', $list['tags']),
        'is_vip' => $list['is_vip'],
        'money' => $list['money'],
        'total_paid' => $list['total_paid'],
        'total_recharge' => $list['total_recharge'],
        'total_geted_reward' => $list['total_geted_reward'],
        'is_ban' => $list['is_ban'],
        'level' => $list['level'],
        'role' => $list['role'],
        'referrer' => $list['referrer'],
        'create_time' => $list['create_time'],
        'update_time' => $list['update_time'],
        'vip_effective_time' => $list['vip_effective_time'],
        'format_vip_effective_time' => $dateTime->timestamp2date($list['vip_effective_time']),
        'format_create_time' => $dateTime->timestamp2date($list['create_time']),
        'format_update_time' => $dateTime->timestamp2date($list['update_time']),
    ];
}

//用户
class UserController extends BaseController
{
    public function get(Request $request, UserQueryService $userQueryService)
    {
        $all = $request->get('all', false);
        $page = $request->get('page', 1);
        $perPage = $request->get('perPage', config('app.allow_limit'));
        $tags = $request->get('tags', '');
        $keyword = $request->get('keyword', '');
        $role = $request->get('role', '');
        $level = $request->get('level', '');
        $search_start_time = $request->get('start_time', '');
        $search_end_time = $request->get('end_time', '');
        $is_vip = $request->get('is_vip', '');
        try
        {
            validate(UserValidate::class)
                ->scene('get')
                ->check([
                    'page'         => $page ,
                    'perPage'      => $perPage ,
                ]);
        } catch (ValidateException $e) {
            throw new ParamException($e->getMessage());
        }

        $user = $userQueryService->getList([
            'page'      =>  $page,
            'perPage'   =>  $perPage,
            'keyword'   =>  $keyword,
            'is_vip'    =>  $is_vip,
            'role'   =>  $role,
            'level'   =>  $level,
            'tags'      =>  $tags,
            'search_start_time'  =>  $search_start_time,
            'search_end_time'    =>  $search_end_time,
            'all'    =>  $all
        ]);

        return $this->returnList(
            array_map(fn($list) => user2dict($list), $user['list']->toArray()),
            $user['pagination']['page'], $user['pagination']['perPage'], $user['pagination']['total']
        );
    }

    public function updateUserTags(Request $request, UserService $userService)
    {
        $tags = $request->param('tags', '');

        if (empty($tags))
        {
            throw new ParamException('未修改任何数据');
        }

        $user = $userService->updateUserTags($tags);

        return $this->returnJson(user2dict($user));
    }

    public function banOrUndo(Request $request, UserService $userService)
    {
        $status = $request->param('status', 1);

        if ($status == 0)
        {
            $user = $userService->undo();
        }else{
            $user = $userService->ban();
        }

        return $this->returnJson(user2dict($user));
    }

    public function getById(ShopUser $shopUser, UserQueryService $userQueryService)
    {
        $user = $userQueryService->getByUser($shopUser);
        return $this->returnJson(user2dict($user));
    }
}